package com.weng.coder.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.common.utils.Constants;
import com.weng.coder.entity.Field;
import com.weng.framework.core.model.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.weng.coder.service.FieldService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 字段 Web API
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-24 20:39:08
 */
@RestController
@RequestMapping("/field") 
@Api(value = "字段相关接口")
public class FieldController{
	@Resource
	private FieldService fieldService;
	
	
	 /**
	 * 获取分页数据
	 */
	 @ResponseBody
	 @RequestMapping(value="/list",method=RequestMethod.GET)
	 @ApiOperation(value = "分页获取字段列表", notes = "分页获取字段列表")
	 @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query",name = "pageNo",dataType ="Integer" ,required = true,value = "页数"),
        @ApiImplicitParam(paramType = "query",name = "pageSize",dataType ="Integer" ,required = true,value = "每页数量")
	 })
	 public ResponseData getDataByPage(String tableId, String name, Integer pageNo, Integer pageSize)throws Exception{
		 Field field = new Field();
		 tableId = StringUtils.isEmpty(tableId) ? "-1" : tableId;
		 field.setTableId(tableId);
		 field.setName(name);
		 IPage<Field> page = fieldService.getDataByPage(pageNo, pageSize, field);
		 return ResponseData.success(page);
	 }
	
	 /**
	 * 获取字段
	 * 
	 */
	@RequestMapping(value="get-one",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取字段", notes = "获取字段")
	public ResponseData getField(String id, Model model
			,HttpServletRequest request) throws Exception{
		if(StringUtils.isEmpty(id)){
			return ResponseData.failed("字段Id不能为空！");
		}
		
		Field field = this.fieldService.getOne(id);
		return ResponseData.success(field);
	}
	 
	/**
	 * 添加字段
	 * 
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加字段", notes = "添加字段")
	public ResponseData addField(Field field, Model model
			,HttpServletRequest request) throws Exception{
		if(field==null ){
			return ResponseData.failed("字段不能为空！");
		}
		if(field.getIsPrimary()==null){
			field.setIsPrimary(Constants.FLAG_TRUE);
		}
		fieldService.save(field);
		return ResponseData.success("添加字段成功！");
	}
	
	/**
	 * 更新字段
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新字段", notes = "更新字段")
	public ResponseData updateField(Field field,HttpServletRequest request) throws Exception{
		if(field==null ){
			return ResponseData.failed("字段不能为空！");
		}
		fieldService.save(field);
		return ResponseData.success("更新成功！");
	}
	
	
	/**
	 * 删除字段
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除字段", notes = "删除字段,多个ID以,号分隔")
	@Transactional
	public  ResponseData deleteField( String ids
			,HttpServletRequest request) throws Exception{
		String[] fieldIdList = ids.split(",");
		if(fieldIdList == null || fieldIdList.length < 1){
			return ResponseData.failed("字段ID不能为空");
		}
		StringBuilder logDesc = new StringBuilder();
		List<Field> fieldList = fieldService.findByIds(Arrays.asList(fieldIdList));
		if(fieldList!=null && fieldList.size()>0){
			for(Field field:fieldList){
				if(logDesc.length()>0){
					logDesc.append(",");
				}
				logDesc.append(field.getId());
				this.fieldService.delete(field);
			}
			request.setAttribute(Constants.AOP_LOG_DESCRIPTION, "删除字段【"+logDesc.toString()+"】");
		}
		return ResponseData.success("删除成功");
	}
	
	/**
	 * 通过SQL添加列属性
	 * 
	 */
	@RequestMapping(value="add-fields-by-sql",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "通过建库SQL添加列属性", notes = "通过建库SQL添加列属性")
	public ResponseData addFieldsBySql(String tableId,String sql, Model model
			,HttpServletRequest request) {
		ResponseData result = this.fieldService.addFieldsBySql(tableId, sql, request);
		return result;
	}


}

